import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';

import 'package:get/get.dart';

import '../controllers/input_controller.dart';

class InputView extends GetView<InputController> {
  static const List<String> _kOptions = <String>[
    'aardvark',
    'bobcat',
    'chameleon',
  ];

  final GlobalKey<FormState> _formKey = GlobalKey();

  ///演示下 form的使用
  Widget _buildForm() {
    return Container(
      child: Form(
        child: Column(
          children: [
            TextFormField(
              validator: (String? str) {
                if (str?.isEmpty ?? false) {
                  return "Please enter some text";
                }
                return null;
              },
              decoration: InputDecoration(
                labelText: "用户名: ",
                hintText: "Enter your email",
              ),
            ),
            Padding(
              padding: EdgeInsets.all(10),
              child: ElevatedButton(
                  onPressed: () {
                    //校验不通过
                    if (!_formKey.currentState!.validate()) {
                      Fluttertoast.showToast(msg: "Please enter some text!");
                      return;
                    }
                  },
                  child: Text("submit")),
            ),
          ],
        ),
        key: _formKey,
      ),
    );
  }

  //根据输入的内容，筛选出相似的
  Widget _buildAutoComplete() {
    return Autocomplete<String>(optionsBuilder: (TextEditingValue text) {
      if (text.text == '') return Iterable.empty();
      return _kOptions.where((String option) {
        print("text ${text.text} ${option}");
        return option.contains(text.text.toLowerCase());
      });
    }, onSelected: (value) {
      print("autocomplete select $value");
    },);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Simple form input'),
        centerTitle: true,
      ),
      body: Column(
        children: [
          _buildForm(),
          _buildAutoComplete(),
        ],
      ),
    );
  }
}
